import 'package:get/get.dart';
import 'package:pornhub_app/components/easy_toast.dart';
import 'package:pornhub_app/http/base_repsponse_model.dart';
import 'package:pornhub_app/http/http_service.dart';
import 'package:pornhub_app/model/engagement/engagement_detail_model.dart';
import 'package:pornhub_app/utils/logger.dart';

class EngagementDetailPageController extends GetxController {
  var meetUserId = 0;

  final engagementModel = EngagementDetailModel().obs;
  RxList<BgImgs> bgImages = <BgImgs>[].obs;
  RxList<String> serviceTags = <String>[].obs;
  var isLike = false.obs;

  @override
  void onInit() {
    super.onInit();
  }

  void initDatas() {
    meetUserId = int.tryParse(Get.parameters['meetUserId'] ?? '') ?? 0;
    getInfoById();
  }

  /// 获取信息
  Future getInfoById() async {
    try {
      final response = await httpInstance.get(
          url: "meet/user/dtl",
          queryMap: {
            'meetUserId': meetUserId,
          },
          complete: EngagementDetailModel.fromJson);
      if (response != null) {
        engagementModel.value = response;
        bgImages.value = engagementModel.value.bgImgs ?? [];
        serviceTags.value = engagementModel.value.serviceTags ?? [];
        isLike.value = engagementModel.value.isAttention ?? false;
      }
    } catch (e) {
      logger.d(e);
    }
  }

  likeOrCancel() async {
    try {
      //已经关注则取消关注
      final response = await httpInstance.post(
        url: isLike.value == true ? "meet/user/like/cancel" : "meet/user/like",
        body: {
          'meetUserId': meetUserId,
        },
      );
      getInfoById();
    } catch (e) {
      logger.d(e);
    }
  }

  unlock() async {
    try {
      final response = await httpInstance.post(
        url: "meet/user/unlock",
        body: {
          'meetUserId': meetUserId,
        },
      );
      getInfoById();
    } catch (e) {
      EasyToast.show("解锁失败");
    }
  }
}
